using System;
using System.Collections.Generic;
using System.Text;
using Evolution.Engine;
using Evolution.Engine.Interfaces;
using Evolution.Engine.GUI.Components;
using Evolution.Engine.Objects;
using System.Collections.ObjectModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Evolution.Engine.Managers;

namespace Evolution.GUI.Components
{
    public class MouseEventArgs
    {
        private MouseState _State;
        private bool _Handled;
        private MouseButton _Button;

        public MouseButton Button { get { return _Button; } set { _Button = value; } }
        public bool Handled { get { return _Handled; } set { _Handled = value; } }
        public MouseState MouseState { get { return _State; } set { _State = value; } }

        public MouseEventArgs(MouseState state, MouseButton button)
        {
            _State = state;
            _Button = button;
            _Handled = false;
        }
    }
    public class KeyEventArgs
    {
        private bool _Handled;
        private KeyList _Keys;
        private KeyboardState _KeyboardState;

        public KeyboardState KeyboardState { get { return _KeyboardState; } set { _KeyboardState = value; } }
        public KeyList Keys { get { return _Keys; } set { _Keys = value; } }
        public bool Handled { get { return _Handled; } set { _Handled = value; } }

        public KeyEventArgs(KeyList list, KeyboardState state)
        {
            _Keys = list;
            _KeyboardState = state;
        }
    }

    public class Form
    {
        protected ControlCollection _Controls = new ControlCollection();
        protected Texture2d _Background;
        protected string _Name = "";
        protected Vector2 _Location = new Vector2();
        protected SizeF _Size = new SizeF(0, 0);
        protected string _Text = "";
        protected object _Tag = null;
        protected int _ZValue = 100;
        protected bool _Visible;
        protected bool _Enabled;
        
        protected bool _FrameBorderEnabled;
        protected Caption _Caption;
        protected Control _ActiveControl;

        public Control ActiveControl { get { return _ActiveControl; } set { _ActiveControl = value; } }
        public bool FrameBorderEnabled { get { return _FrameBorderEnabled; } set { _FrameBorderEnabled = value; } }
        public bool CloseButtonEnabled { get { return _Caption.CloseButtonEnabled; } set { _Caption.CloseButtonEnabled = value; } }
        public bool CaptionEnabled { get { return _Caption.Enable; } set { _Caption.Enable = value; } }

        public bool Enabled { get { return _Enabled; } set { _Enabled = value; } }
        public bool Visible { get { return _Visible; } set { _Visible = value; } }
        public Rectangle Bounds { get { return new Rectangle((int)_Location.X, (int)_Location.Y, (int)_Size.Width, (int)_Size.Height); } }
        public int ZValue { get { return _ZValue; } set { _ZValue = value; } }
        public object Tag { get { return _Tag; } set { _Tag = value; } }
        public string Text { get { return _Text; } set { _Text = value; } }
        public SizeF Size { get { return _Size; } set { _Size = value; Resize(); } }
        public Vector2 Location { get { return _Location; } set { _Location = value; UpdateLocation(); } }
        public string Name { get { return _Name; } set { _Name = value; } }
        public Texture2d Background { get { return _Background; } set { _Background = value; } }
        public ControlCollection Controls { get { return _Controls; } set { _Controls = value; } }

        #region Form Members
        public Form(string name)
        {
            _Name = name;
            _Visible = true;
            _Caption = new Caption(this);
            _Caption.Enable = true;
            SetBackgroundImage("Content/GUI/Form/FormBackground");
        }

        public void Show()
        {
            _Visible = true;
        }
        public void Hide()
        {
            _Visible = false;
        }
        public void Enable()
        {
            _Enabled = true;
        }
        public void Disable()
        {
            _Enabled = false;
        }
        public void SetBackgroundImage(string imageName)
        {
            _Background = new Texture2d(new Vector2(), new Vector2(64, 64), imageName);
            _Background.LoadContent();
        }
        public void SetCaption(string Title)
        {
            _Caption.Title = Title;
            _Caption.Enable = true;
        }

        public void SetActiveControl(Control control)
        {
            if (_ActiveControl != null)
                _ActiveControl.OnFocusLost();

            _ActiveControl = control;
            control.Focus = true;
        }

        #endregion

        #region Update Members

        public virtual void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            foreach (Control con in _Controls)
                con.Update(gameTime);

            if (_Caption.Enable) _Caption.Update(gameTime);
        }

        #endregion

        #region ILoadable Members

        public virtual void LoadContent()
        {
            foreach (Control con in _Controls)
                con.LoadContent();
        }

        #endregion

        #region Renderer Members

        public virtual void Draw(GameTime gameTime)
        {
            if (_Background != null) _Background.Draw(gameTime);
            foreach (Control con in _Controls)
                con.Draw(gameTime);
            if (_Caption.Enable) _Caption.Draw(gameTime);
        }

        #endregion

        #region Mouse Functions

        public void OnMouseDown(MouseEventArgs e)
        {
            if (_Caption.Enable)
            {
                _Caption.OnMouseDown(e);
                if (e.Handled)
                    return;
            }

            foreach (Control con in _Controls)
            {
                con.OnMouseDown(e);
                if (e.Handled)
                    return;
            }
        }
        public void OnMouseUp(MouseEventArgs e)
        {
            if (_Caption.Enable)
            {
                _Caption.OnMouseUp(e);
                if (e.Handled)
                    return;
            } 
            
            foreach (Control con in _Controls)
            {
                con.OnMouseUp(e);
                if (e.Handled)
                    return;
            }
        }
        public void OnKeyDown(KeyEventArgs e)
        {
            foreach (Control con in _Controls)
            {
                con.OnKeyDown(e);
                if (e.Handled)
                    return;
            }
        }

        private void Resize()
        {
            if (_Caption.Enable) _Caption.Size = new SizeF(_Size.Width, 32);
            _Background.Size = new Vector2(
                    _Size.Width,
                    _Size.Height
                );
        }
        private void UpdateLocation()
        {
            if (_Caption.Enable) _Caption.Location = new Vector2(_Location.X - 10, _Location.Y - _Caption.Size.Height);
            _Background.Position = _Location;
        }

        #endregion

        public void OnDragging(MouseEventArgs mouseargs)
        {
            if (_Caption.Enable)
                _Caption.OnDragging(mouseargs);
        }
    }

    public class FormCollection : Collection<Form> 
    {
        public Form FindByName(string Name)
        {
            foreach (Form form in this.Items)
            {
                if (form.Name == Name)
                    return form;
            }

            return null;
        }
    }
}
